package inl.resilienceManager.qmul.gemom;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.tree.DefaultMutableTreeNode;

import resilienceManager.qmul.gemom.Gbroker;
import resilienceManager.qmul.gemom.NSitem;

/** COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 * */

public class ACaseSearch {
	
	public  HashMap<Gbroker, solution> g2s;
	
	public ACaseSearch( solution[] s, Gbroker[] glist){
		g2s = getGbrokerSol(glist, s);
	}
//	private int[][] sets;
	
//	public ABuildCB(Gbroker[] brokers){
//		int[] brokerAlive = new int[brokers.length];
//		casteTree(brokers);
////		combinations of failed brokers
//	}
	
	public static LinkedList<LinkedList<Integer>> caseTree(Gbroker[] brokers){
//FIXME the returned LL<LL>may have an empty ll inside  (1st in list) 

		LinkedList<Integer> ibrokers=new LinkedList<Integer>(); 
// brokers[ibrokers.get(i).intValue()];
		for(int i=0; i<brokers.length; i++)
		{
			ibrokers.add((Integer)i);

		}
		
		
		
//		combinations of failed brokers
		LinkedList<LinkedList<Integer>> fbs= AuctionTree.powSet(ibrokers);
		return fbs;
	}
	
	static solution[] handle1Case(solution[] s, Gbroker[] glist,  int[] fb, NSitem[] total){
		if(fb!=null && fb.length>0){
			//		item from failed brokers - fb
			//		live brokers
			Gbroker[] gblist;
			Gbroker[] deadlist;


			ArrayList<Gbroker> b_dead = new ArrayList<Gbroker>(); 
			ArrayList<Gbroker> b = new ArrayList<Gbroker>(Arrays.asList(glist));
			for (int i=0; i<fb.length; i++){
				b_dead.add(glist[fb[i]]);
				b.remove(fb[i]);
			}
			deadlist = (Gbroker[]) b_dead.toArray();
			//			b.removeAll(b_dead);
			gblist= (Gbroker[]) b.toArray();


			
			
			
			//		items to be allocated
			NSitem[] t = getDeadItems(deadlist,s);

			AuctionTree bt= new AuctionTree(t,gblist,0);

			bt.buildTree();



			DefaultMutableTreeNode root=bt.SearchTree();

			DefaultMutableTreeNode currentnode=root;


			//		solution?

			String[] solution= new String[glist.length];

			solution[] slnew= new solution[glist.length];

			for(int i=0; i<glist.length; i++)
			{

				DefaultMutableTreeNode inode=(DefaultMutableTreeNode) currentnode.getChildAt(0);

				// save the allocation
				solution[i]=inode.toString();

				slnew[i]= new solution(glist[i].getID(),solution[i], total);

				currentnode=inode;

			}

			return slnew;	
		}
		else {
			System.out.println("failed brokers :" + fb);
			return null;
		}
	}
	
	
//XXX return null if ll is null or  isEmpty()i.e. size=0
		public static int[] ll2Int(LinkedList<Integer> ll){
			if(ll!=null && ll.size()>0){
				int [] gi = new int[ll.size()];
				for(int i=0; i<ll.size(); i++){
					gi[i] = ll.get(i).intValue();
				}
				return gi;
			}
			else return null;
		}

		static solution getGbrokerSol(Gbroker gb, solution[] s){
			solution ss;
			for(int i=0; i<s.length; i++){
				if (s[i].getGID()==gb.getID()){
					return s[i];
				}
			}
			return null;
		}
		static NSitem[] getGbrokerItems(Gbroker gb, solution[] s){
			NSitem[] nsi=null;
			for(int i=0; i<s.length; i++){
				if (s[i].getGID()==gb.getID()){
					nsi = s[i].getAlist();
				}
				else {
					System.out.println("no solution for broker:" + gb.getID());
				}
			}
			return nsi;
		}

		static HashMap<Gbroker, solution> getGbrokerSol(Gbroker[] gb, solution[] s){
			HashMap<Gbroker, solution> gbsol =new HashMap<Gbroker, solution>();
			for (int i=0; i<gb.length; i++){

				for(int j=0; j<s.length; j++){
					if (s[j].getGID()==gb[i].getID()){
						gbsol.put(gb[i], s[j]);
					}
				}
			}
			return gbsol;
		}

		static NSitem[] getDeadItems(Gbroker[] gb, solution[] s){
			//		HashMap<Gbroker, solution> gbsol =new HashMap<Gbroker, solution>();
			ArrayList<NSitem> ss = new ArrayList<NSitem>();
			for (int i=0; i<gb.length; i++){

				for(int j=0; j<s.length; j++){
					if (s[j].getGID()==gb[i].getID()){
						ss.addAll(Arrays.asList(s[j].getAlist()));
					}
				}
			}
			return (NSitem[]) ss.toArray();
		}
		
		static void updateBrokers(Gbroker[] b, solution[] s){
		}


		//	public handle(){
		//		
		//	}

		/*	ini primary
		updatestate

	df search 
	context brokers alive
	tree for bi b1-bn{
		tree(bi - items, rest bs)
		get closed bs
		get 
		for current state of brokers (items)

	}

	if mean > C -
	//emergency - carry all? the best	
	utiltiy = second risk

	mirror == update all solutions (b1-bn one fails)

	updatestate (solution, brokers)
	risk (item, state)

	table: context, (more context), b1 -bn

		 */
	
}
